package com.feonix.common.basedao;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class InsertSql {
	private Long pkValue;
	private String pkColumn;
	private String tableName;
	private boolean hasPk = true;

	private Map<String, Object> params = new HashMap<String, Object>();

	public InsertSql(String tableName) {
		this.tableName = tableName;
	}

	public InsertSql(String tableName, String pkColumn) {
		this.tableName = tableName;
		this.pkColumn = pkColumn;
	}

	public InsertSql(String tableName, boolean hasPk) {
		this.tableName = tableName;
		this.hasPk = hasPk;
	}

	public void addParam(String key, Object value) {
		this.params.put(key, value);
	}

	public void addString(String key, String value) {
		this.params.put(key, value);
	}

	public void addInt(String key, int value) {
		this.params.put(key, Integer.valueOf(value));
	}

	public void addDate(String key, Date value) {
		this.params.put(key, value);
	}

	public void addDouble(String key, double value) {
		this.params.put(key, Double.valueOf(value));
	}

	public void addLong(String key, long value) {
		this.params.put(key, Long.valueOf(value));
	}

	public void addFloat(String key, float value) {
		this.params.put(key, Float.valueOf(value));
	}

	protected String getSql() {
		StringBuffer sb = new StringBuffer();
		sb.append("INSERT INTO ").append(this.tableName).append(" (");
		Set<Entry<String, Object>> paramesSet = this.params.entrySet();
		StringBuffer valueSB = new StringBuffer();
		for (Iterator<Entry<String, Object>> iterator = paramesSet.iterator(); iterator.hasNext();) {
			Entry<String, Object> entry = iterator.next();
			valueSB.append(":").append(entry.getKey()).append(", ");
		}
		valueSB.deleteCharAt(valueSB.length() - 2);
		String valueSql = valueSB.toString();
		valueSB.delete(0, valueSB.length());
		sb.append(valueSql.replaceAll(":", "").trim());
		sb.append(") VALUES(").append(valueSql.trim()).append(")");
		String sql = sb.toString();
		sb.delete(0, sb.length());
		return sql;
	}

	protected Map<String, Object> getParams() {
		return this.params;
	}

	public Long getPkValue() {
		return this.pkValue;
	}

	public void setPkValue(Long pkValue) {
		this.pkValue = pkValue;
	}

	public String getPkColumn() {
		return this.pkColumn;
	}

	boolean isHasPk() {
		return this.hasPk;
	}
}
